package com.xhwl.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.enums.BusinessRangeTypeEnum;
import com.xhwl.common.enums.OrganizationTypeEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.config.OrganizationConfig;
import com.xhwl.common.query.OrganizationConfigQuery;
import com.xhwl.data.service.IOrganizationConfigFeignService;
import com.xhwl.data.service.IOrganizationConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: luyi
 * @Description:
 * @Date: Create in 17:19 2021/10/25
 */
@Service
public class OrganizationConfigServiceImpl implements IOrganizationConfigService {

	@Autowired
	private IOrganizationConfigFeignService organizationConfigFeignService;

	private static final Logger log = LoggerFactory.getLogger(OrganizationConfigServiceImpl.class);

	@Override
	public List<OrganizationConfig> next(OrganizationConfigQuery organizationConfigQuery, Integer type) {
		// 根据项目查询，没有就返回企业的配置，再没有返回业态的配置
		List<OrganizationConfig> list = organizationConfigFeignService.list(organizationConfigQuery);
		if (list.isEmpty()) {
			log.error("获取组织配置信息失败");
			return Collections.emptyList();
		}
		if (null == type) {
			return Collections.emptyList();
		}

		// 项目范围的配置不会新增项目结点，根据项目去查询的时候默认项目的下级就是 level = 1 层级的类型
		if (BusinessRangeTypeEnum.PROJECT == list.get(0).getRangeType() && type == OrganizationTypeEnum.PROJECT.id.intValue()) {
			list = list.stream().filter(l -> l.getLevel().intValue() == 1).collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(OrganizationConfig::getType))), ArrayList::new));
			return list;
		}

		List<Integer> ids = list.stream().filter(l -> type == l.getType().intValue()).map(l -> l.getId()).collect(Collectors.toList());
		return list.stream().filter(l -> ids.contains(l.getParentId())).collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(OrganizationConfig::getType))), ArrayList::new));
	}

	@Override
	public List<OrganizationConfig> parent(OrganizationConfigQuery organizationConfigQuery, Integer type) {

		// 根据项目查询，没有就返回企业的配置，再没有返回业态的配置
		List<OrganizationConfig> list = organizationConfigFeignService.list(organizationConfigQuery);
		if(list.isEmpty()){
			log.info("获取不到空间配置信息 {}", JSONObject.toJSONString(organizationConfigQuery));
			return Collections.emptyList();
		}
		List<OrganizationConfig> target = list.stream().filter(i -> i.getType().intValue() == type.intValue()).collect(Collectors.toList());

		if(target.isEmpty()){
			log.info("空间 {} 暂未配置", type);
			return Collections.emptyList();
		}
		List<OrganizationConfig> result = new ArrayList<>();
		// 如果rangetype是项目 并且 organizationConfig level = 1 的时候 需要填充上级为项目
		List<OrganizationConfig> collect1 = target.stream().filter(i -> BusinessRangeTypeEnum.PROJECT == i.getRangeType() && 1 == i.getLevel()).collect(Collectors.toList());
		if(!collect1.isEmpty()){
			// 这里填充项目
			OrganizationConfig organizationConfig = new OrganizationConfig();
			organizationConfig.setName("项目");
			organizationConfig.setParentId(-1);
			organizationConfig.setId(0);
			organizationConfig.setProjectId(organizationConfigQuery.getProjectId());
			organizationConfig.setIndustryId(collect1.get(0).getIndustryId());
			organizationConfig.setRangeType(BusinessRangeTypeEnum.PROJECT);
			organizationConfig.setType(OrganizationTypeEnum.PROJECT.id.intValue());
			result.add(organizationConfig);
		}

		List<Integer> collect = target.stream().map(i -> i.getParentId()).collect(Collectors.toList());
		List<OrganizationConfig> collect2 = list.stream().filter(i -> collect.contains(i.getId())).collect(Collectors.toList());
		result.addAll(collect2);
		return result;
	}
}
